package com.xiaozhengkeji.blogsuper.api;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.xiaozhengkeji.blogsuper.base.entitys.ArticleSTEntity;
import com.xiaozhengkeji.blogsuper.base.service.ArticleClassEntityService;
import com.xiaozhengkeji.blogsuper.base.service.ArticleSTEntityService;
import com.xiaozhengkeji.blogsuper.base.service.SystemConfigEntityService;
import com.xiaozhengkeji.blogsuper.base.service.UserEntityService;
import com.xiaozhengkeji.blogsuper.bean.ResponseBean;
import com.xiaozhengkeji.blogsuper.bean.type.ResCodeType;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 文章Api
 */
@Slf4j
@RestController
@RequestMapping("/web/Article/core")
public class ArticleApi {
    @Autowired
    UserEntityService userEntityService;
    @Autowired
    ArticleClassEntityService articleClassEntityService;
    @Autowired
    SystemConfigEntityService systemConfigEntityService;
    @Autowired
    ArticleSTEntityService articleSTEntityService;

    /**
     * 创建一个文章
     */
    @ApiOperation("创建一个文章 需要权限(admin:all)")
    @GetMapping("/create")
    @SaCheckPermission("admin:all")
    public ResponseBean createArticle(@ApiParam("文章分类") @RequestParam("ArticleClassID") Integer ArticleClassID,
                                      @ApiParam("文章标题") @RequestParam("ArticleTagsName") String ArticleTagsName,
                                      @ApiParam("文章封面URL") @RequestParam("ArticleCoverImgURL") String ArticleCoverImgURL,
                                      @ApiParam("文章备注") @RequestParam("ArticleRemark") String ArticleRemark,
                                      @ApiParam("文章内容") @RequestParam("ArticleContent") String ArticleContent) {
        List<ArticleSTEntity> Temp_ArticleSTEntity = articleSTEntityService.queryByArticleTagsName(ArticleTagsName);
        if (Temp_ArticleSTEntity.size() > 0) {
            return new ResponseBean(ResCodeType.Error, "已经存在此文章标题", null, null);
        }
        ArticleSTEntity Temp_Deposit_Base_ArticleSTEntity = new ArticleSTEntity();
        Temp_Deposit_Base_ArticleSTEntity.setArticleClassID(ArticleClassID);
        Temp_Deposit_Base_ArticleSTEntity.setArticleStatus(-1);
        Temp_Deposit_Base_ArticleSTEntity.setArticleTagsName(ArticleTagsName);
        Temp_Deposit_Base_ArticleSTEntity.setArticleCoverImgURL(ArticleCoverImgURL);
        Temp_Deposit_Base_ArticleSTEntity.setArticleRemark(ArticleRemark);
        Temp_Deposit_Base_ArticleSTEntity.setArticleReadCount(1);
        Temp_Deposit_Base_ArticleSTEntity.setArticleFavorite(1);
        Temp_Deposit_Base_ArticleSTEntity.setArticleContent(ArticleContent);
        Temp_Deposit_Base_ArticleSTEntity.setArticleCreateTime(LocalDateTime.now());
        Temp_Deposit_Base_ArticleSTEntity.setArticleModifyTime(LocalDateTime.now());
        return new ResponseBean(ResCodeType.Success, "创建文章完成", articleSTEntityService.insert(Temp_Deposit_Base_ArticleSTEntity), null);
    }

    /**
     * 修改一个文章的状态
     */
    @ApiOperation("修改一个文章的状态 需要权限(admin:all)")
    @GetMapping("/modifyArticleStatus")
    @SaCheckPermission("admin:all")
    public ResponseBean modifyArticleStatus(@ApiParam("文章ID") @RequestParam("ArticleID") Integer ArticleID,
                                            @ApiParam("文章状态") @RequestParam("ArticleStatus") Integer ArticleStatus) {
        ArticleSTEntity Temp_ArticleSTEntity = articleSTEntityService.selectByPrimaryKey(ArticleID);
        if (Temp_ArticleSTEntity == null) {
            return new ResponseBean(ResCodeType.Error, "不存在此文章", null, null);
        }
        Temp_ArticleSTEntity.setArticleStatus(ArticleStatus);
        return new ResponseBean(ResCodeType.Success, "修改完成!", null, null);
    }


    /**
     * 获取一个分类的下的文章
     */
    @ApiOperation("获取一个分类的下的文章")
    @GetMapping("/getClassAllArticle")
    public ResponseBean getClassAllArticle(@ApiParam("文章分类") @RequestParam("ArticleClassID") Integer ArticleClassID) {
        List<ArticleSTEntity> Temp_ArticleSTEntities = articleSTEntityService.queryByArticleClassID(ArticleClassID);
        return new ResponseBean(ResCodeType.Success, "ok!", Temp_ArticleSTEntities, null);
    }

    /**
     * 修改文章
     */
    @ApiOperation("修改文章")
    @PostMapping("/modifyArticle")
    @SaCheckPermission("admin:all")
    public ResponseBean modifyArticle(@ApiParam("文章ID") @RequestParam("ArticleID") Integer ArticleID,
                                      @ApiParam("文章分类") @RequestParam("ArticleClassID") Integer ArticleClassID,
                                      @ApiParam("文章标题") @RequestParam("ArticleTagsName") String ArticleTagsName,
                                      @ApiParam("文章封面URL") @RequestParam("ArticleCoverImgURL") String ArticleCoverImgURL,
                                      @ApiParam("文章备注") @RequestParam("ArticleRemark") String ArticleRemark,
                                      @ApiParam("文章内容") @RequestParam("ArticleContent") String ArticleContent) {
        ArticleSTEntity Temp_ArticleSTEntity = articleSTEntityService.selectByPrimaryKey(ArticleID);
        if (Temp_ArticleSTEntity == null) {
            return new ResponseBean(ResCodeType.Error, "不存在此文章", null, null);
        }
        Temp_ArticleSTEntity.setArticleClassID(ArticleClassID);
        Temp_ArticleSTEntity.setArticleTagsName(ArticleTagsName);
        Temp_ArticleSTEntity.setArticleCoverImgURL(ArticleCoverImgURL);
        Temp_ArticleSTEntity.setArticleRemark(ArticleRemark);
        Temp_ArticleSTEntity.setArticleContent(ArticleContent);
        return new ResponseBean(ResCodeType.Success, "ok!", articleSTEntityService.updateByPrimaryKey(Temp_ArticleSTEntity), null);
    }
}
